package com.mnkjxy.beauty;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.mnkjxy.beauty.view.FirstView;
import com.mnkjxy.beauty.view.CourseView;
import com.mnkjxy.beauty.view.manager.BottomManager;
import com.mnkjxy.beauty.view.manager.MiddleManage;
import com.mnkjxy.beauty.view.manager.TitleManage;

public class MainActivity extends Activity {

	private TitleManage titleManage;
	private BottomManager bottomManager;
	private MiddleManage middleManage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		init();
	}

	public void init() {
		// 初始化标题
		titleManage = TitleManage.geiInstance();
		titleManage.init(this);

		bottomManager = BottomManager.getInstrance();
		bottomManager.init(this);

		// 初始化中间容器
		RelativeLayout middle = (RelativeLayout) findViewById(R.id.middle_container);
		middleManage = MiddleManage.getInstance();
		middleManage.setMiddle(middle);
		// 将中间容器和顶部底部容器相关联,内容观察者
		MiddleManage.getInstance().addObserver(titleManage);
		MiddleManage.getInstance().addObserver(bottomManager);
		// 应用初始化完毕,显示第一个界面
		middleManage.changeView(FirstView.class);
		

	}

	/*
	 * 系统返回键操作,
	 */
	private long FirstKeyDownTime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			boolean result = MiddleManage.getInstance().goBack();
			if (!result) {
				Toast.makeText(this, "双击退出程序", 0).show();
				if (FirstKeyDownTime == 0) {
					FirstKeyDownTime = SystemClock.uptimeMillis();
					return true;
				} else if (FirstKeyDownTime != 0) {
					if ((SystemClock.uptimeMillis() - FirstKeyDownTime) < 1000) {
						android.os.Process.killProcess(android.os.Process
								.myPid());
						// return super.onKeyDown(keyCode, event);
					} else {
						FirstKeyDownTime = SystemClock.uptimeMillis();
					}

				}

			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
